package com.woniuxy.caradmin.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.caradmin.entity.pojo.Outrecord;
import com.woniuxy.caradmin.entity.vo.OutrecordVo;
import com.woniuxy.caradmin.exception.CarException;
import com.woniuxy.caradmin.service.DriverService;
import com.woniuxy.caradmin.service.OutrecordService;
import com.woniuxy.caradmin.service.VehiclefilesService;
import com.woniuxy.caradmin.util.Constaint;
import com.woniuxy.caradmin.util.DataGridView;
import com.woniuxy.caradmin.util.R;
import com.woniuxy.caradmin.util.ResultCode;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.io.OutputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Vector;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author womenzu
 * @since 2020-12-07
 */
@RestController
@RequestMapping("/outrecord")
public class OutrecordController {
    @Autowired
    private VehiclefilesService vehiclefilesService;
    @Autowired
    private DriverService driverService;
    @Autowired
    private OutrecordService outrecordService;

    @GetMapping("/updateoutrecord")
    public ModelAndView updateoutrecord(ModelAndView modelAndView) {
        modelAndView.setViewName("outrecordupdate");
        return modelAndView;
    }

    @GetMapping("/goInrecord")
    public ModelAndView goInrecord(ModelAndView modelAndView) {
        modelAndView.setViewName("goInrecord");
        return modelAndView;
    }

    @GetMapping("/goOut")
    public ModelAndView goOut(ModelAndView modelAndView) {
        modelAndView.setViewName("out");
        return modelAndView;
    }

    @GetMapping("/goOutrecord")
    public ModelAndView goOutrecord(ModelAndView modelAndView) {
        modelAndView.setViewName("goOutrecord");
        return modelAndView;
    }

    @ApiOperation("显示所有的未回车出车记录信息")
    @GetMapping("/outrecordList")
    
    public DataGridView outrecordList(String returntime1, OutrecordVo outrecordVo) {
        Page<Outrecord> page = PageHelper.startPage(outrecordVo.getPage(), outrecordVo.getLimit());
        List<Outrecord> outrecords = outrecordService.selectOutcordByReturntime(null);
        if (outrecords != null && outrecords.size() > 0) {
            return new DataGridView(page.getTotal(), outrecords);
        } else {
            throw new CarException(ResultCode.OUTRECORDRECORDLISTNUll, Constaint.OUTRECORDRECORDLISTNUll);
        }
    }

    @ApiOperation("显示所有的出车记录信息")
    @GetMapping("/outrecordList1")
    @Transactional
    public DataGridView outrecordList1(String vhicelenum, OutrecordVo outrecordVo) {
        Page<Outrecord> page = PageHelper.startPage(outrecordVo.getPage(), outrecordVo.getLimit());
        List<Outrecord> outrecords;
        if (vhicelenum != null && !vhicelenum.equals("")) {
            outrecords = outrecordService.selectOutcordList(vhicelenum);
        } else {
            outrecords = outrecordService.selectOutcordByReturntime(null);
        }
        if (outrecords != null && outrecords.size() > 0) {
            return new DataGridView(page.getTotal(), outrecords);
        } else {
            throw new CarException(ResultCode.OUTRECORDLIST1NULL, Constaint.OUTRECORDLIST1NULL);
        }
    }

    @ApiOperation("增加出行记录信息")
    @GetMapping("/insertOut")
    @Transactional
    public R insertOut(Outrecord outrecord) {
        Boolean result = outrecordService.insertOut(outrecord);
        if (result) {
            //修改车辆状态为不可用
            vehiclefilesService.removeVehicleById(outrecord.getVid());
            //修改驾驶员为不可用
            driverService.delectDriver(outrecord.getDriverid());
            return R.ok().code(ResultCode.INSERTOUT).message(Constaint.INSERTOUT);
        } else {
            return R.error().code(ResultCode.INSERTOUTNULL).message(Constaint.INSERTOUTNULL);
        }
    }

    @ApiOperation("通过id查询一条出行记录信息")
    @GetMapping("/selectOutrecordById")
    public R selectOutrecordById(Integer outrecordid) {
        Outrecord outrecord = outrecordService.selectOutrecordById(outrecordid);
//        Date outtime = outrecord.getOuttime();
//        java.sql.Date outtimesql=new java.sql.Date(outtime.getTime());
//        outrecord.setOuttime(outtimesql);
//        Date predicttime = outrecord.getPredicttime();
//        java.sql.Date  predicttimesql=new java.sql.Date(predicttime.getTime());
//        outrecord.setPredicttime(predicttimesql);
        if (outrecord != null) {
            return R.ok().data("outrecord", outrecord).code(ResultCode.SELETBUID).message(Constaint.SELETBUID);
        } else {
            return R.error().code(ResultCode.SELETBUIDFALSE).message(Constaint.SELETBUIDFALSE);
        }
    }

    @ApiOperation("修改一条出行记录信息")
    @GetMapping("/updateByRecord")
    public R updateByRecord(Outrecord outrecord) {
        boolean result = outrecordService.updateByRecord(outrecord);
        if (result) {
            return R.ok().code(ResultCode.UPDATESUCCESSRECORD).message(Constaint.UPDATESUCCESSRECORD);
        } else {
            return R.error().code(ResultCode.UPDATEFALSERECORD).message(Constaint.UPDATEFALSERECORD);
        }
    }

    @ApiOperation("删除一条出行记录信息")
    @GetMapping("/deletByRecord")
    public R deletByRecord(Integer id) {
        boolean result = outrecordService.deletByRecord(id);
        if (result) {
            return R.ok().code(ResultCode.DELETESUCCESSRECORD).message(Constaint.DELETESUCCESSRECORD);
        } else {
            return R.error().code(ResultCode.DELETEFALSERECORD).message(Constaint.DELETEFALSERECORD);
        }
    }

    @ApiOperation("修改一条回车记录信息")
    @GetMapping("/updateByInRecord")
    @Transactional
    public R updateByInRecord(Outrecord outrecord) {
        boolean result = outrecordService.updateByRecord(outrecord);
        if (result) {
            //修改车辆状态为可用
            vehiclefilesService.updateVehicleById(outrecord.getVid());
            //修改驾驶员为可用
            driverService.updateDriverBuId(outrecord.getDriverid());
            return R.ok().code(ResultCode.UPDATESUCCESSRECORD).message(Constaint.UPDATESUCCESSRECORD);
        } else {
            return R.error().code(ResultCode.UPDATEFALSERECORD).message(Constaint.UPDATEFALSERECORD);
        }
    }

    @ApiOperation("删除所有的信息")
    @PostMapping("/deletePatch")
    @Transactional
    public R deletePatch(Integer[] ids) {
        Integer id;
        boolean result=false;
        for (int i = 0; i <ids.length; i++) {
            id=ids[i];
             result = outrecordService.deletByRecord(id);
        }
        if (result) {
            return R.ok().code(ResultCode.DELETESUCCESSRECORD).message(Constaint.DELETESUCCESSRECORD);
        } else {
            return R.error().code(ResultCode.DELETEFALSERECORD).message(Constaint.DELETEFALSERECORD);
        }
    }
}

